package com.aixingfu.sports.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aixingfu.sports.R;
import com.aixingfu.sports.bean.PaymentBean;
import com.aixingfu.sports.view.MyViewHolder;

import java.util.ArrayList;
import java.util.List;

public class MyPaymentAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<String> mList;
    private List<PaymentBean> paymentBeansList;
    private OnItemEditTextChangedListener mListener;
    public void setListener(OnItemEditTextChangedListener listener) {
        mListener = listener;
    }
    public MyPaymentAdapter(Context context, List<PaymentBean> list) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        paymentBeansList = list;
    }
    public void setList(List<PaymentBean> list) {
        this.paymentBeansList = list;
        notifyDataSetChanged();
    }
    public void seData(List<PaymentBean> list) {
        this.paymentBeansList = list;
    }
    public interface OnItemEditTextChangedListener {
        void onEditTextAfterTextChanged(Editable editable, int position);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(mInflater.inflate(R.layout.item_payment_my, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        holder.setIsRecyclable(false);
        final MyViewHolder viewHolder = (MyViewHolder) holder;
        String payment = paymentBeansList.get(position).getPayment();
        if ("请选择支付方式".equals(payment)){
            viewHolder.content.setHint(paymentBeansList.get(position).getPayment());
        }else {
            viewHolder.content.setText(paymentBeansList.get(position).getPayment());
        }
        viewHolder.money.setText(paymentBeansList.get(position).getPrice());
        viewHolder.money.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                mListener.onEditTextAfterTextChanged(s, position);
            }
        });

        viewHolder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.onItemClick(position,v);
            }
        });
    }

    @Override
    public int getItemCount() {
        return paymentBeansList != null ? paymentBeansList.size() : 0;
    }

    public void removeItem(int position) {
        paymentBeansList.remove(position);
        notifyDataSetChanged();
    }
    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
}
